package com.twang.stock.service.jinyan;


import com.twang.stock.dto.BaseRequest;
import com.twang.stock.enmu.UserBusinessTypeEnum;
import com.twang.stock.entity.CompanyCodeInfo;
import com.twang.stock.entity.CompanyCodeInfoEx;
import com.twang.stock.entity.User;
import com.twang.stock.mapper.CompanyCodeInfoMapper;
import com.twang.stock.service.UserService;
import com.twang.stock.task.old.QinXuGaoDuTask;
import com.twang.stock.utils.date.HolidayCopy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class CompanyInfoService {

    @Resource
    private CompanyCodeInfoMapper companyCodeInfoMapper;

    @Autowired
    private QinXuGaoDuTask qinXuGaoDuTask;
    @Autowired
    private UserBusinessTypeService userBusinessTypeService;

    @Autowired
    private UserService userService;

    @Autowired
    private HolidayService holidayService;


    /**
     *
     * @return
     */
    public List<CompanyCodeInfoEx> query() {
        log.info("start 查询 time={}",HolidayCopy.getHHmmss());
        User user = userService.getUserByToken();
        List<CompanyCodeInfoEx> companyCodeInfoList =companyCodeInfoMapper.getStockInfo(holidayService.getDayByNum(0));
        //已经关注的
        List<Long> companyCodeInfos = userBusinessTypeService.selectStockIds(user.getId(), UserBusinessTypeEnum.TYPE_STOCK_INFO.getCode());
        log.info("start 查询 开始 time={}",HolidayCopy.getHHmmss());
        for (CompanyCodeInfoEx item : companyCodeInfoList) {
            //判断用户关注的信息
            {
                if (CollectionUtils.isEmpty(companyCodeInfos)) {
                    item.setType(0);
                } else {
                    for (Long v : companyCodeInfos) {
                        if (item.getId().intValue() == v.intValue()) {
                            item.setType(1);
                            break;
                        } else {
                            item.setType(0);
                        }
                    }
                }
            }
        }
        log.info("end 查询   time={}",HolidayCopy.getHHmmss());
        return companyCodeInfoList;
    }

    public void setType(BaseRequest request) throws Exception {
        Boolean flag=Boolean.FALSE;
        if (null == request.getId() && StringUtils.isEmpty(request.getCode())) {
            throw new Exception("id 或 code 不能为空！");
        }
        Example example = new Example(CompanyCodeInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != request.getId()) {
            criteria.andEqualTo("id", request.getId());
        }
        if (StringUtils.isNotEmpty(request.getCode())) {
            criteria.andEqualTo("companyCode", request.getCode());
        }
        List<CompanyCodeInfo> records = companyCodeInfoMapper.selectByExample(example);
        CompanyCodeInfo record;
        if (CollectionUtils.isEmpty(records)) {
            throw new Exception("未查询到数据！");
        }
        record = records.get(0);
        record.setType(request.getType());
        companyCodeInfoMapper.updateByPrimaryKeySelective(record);
        //
        if(1==request.getType()){
            userBusinessTypeService.insertUserBusinessType(record.getId().longValue(), UserBusinessTypeEnum.TYPE_STOCK_INFO.getCode(),HolidayCopy.getRealToday());
        }else{
            userBusinessTypeService.deletedUserBusinessType(record.getId().longValue(), UserBusinessTypeEnum.TYPE_STOCK_INFO.getCode(),null);
        }
        if("update".equals(request.getFlag())){
            qinXuGaoDuTask.initChangXianChi();
        }

    }
    /**
     *
     * @return
     */
    public List<CompanyCodeInfo> granteTodayStockInfo(){
        return   companyCodeInfoMapper.granteTodayStockInfo();
    }

    /**
     *
     * @return
     */
    public List<CompanyCodeInfo> granteTodayStockDesc(){
        return   companyCodeInfoMapper.granteTodayStockDesc();
    }

    /**
     *
     * @return
     */
    public List<CompanyCodeInfo> granteTodayStockMoneyIn50(){
        return   companyCodeInfoMapper.granteTodayStockMoneyIn50();
    }

    /**
     *
     * @return
     */
    public List<CompanyCodeInfo> granteTodayStockMoneyInDays(){
        return   companyCodeInfoMapper.granteTodayStockMoneyInDays();
    }

}
